package compiler.node;

import org.antlr.runtime.Token;

import java.util.Stack;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
public abstract class Operator extends Node {
    public Operator(Token token) {
        super(token);
    }

    protected void execute(Stack stack) {
        super.execute(stack);
        boolean[] a1 = (boolean[]) stack.pop();
        boolean[] a2 = (boolean[]) stack.pop();
        boolean[] result = doOperation(a1, a2);
        stack.push(result);
    }

    protected boolean[] doOperation(boolean[] a1, boolean[] a2) {
        boolean[] result = new boolean[a1.length];
        for (int j = 0; j < a1.length; j++) {
            result[j] = doOperation(a1[j], a2[j]);
        }
        return result;
    }

    abstract protected boolean doOperation(boolean a1, boolean a2);
}
